<?php

/*
	工单列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=$this->task_types;
$return['statuss']=$this->task_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//类型。1表示开发工单，2表示bug修复工单，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//工单名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_workliu_task',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['member']=cfc('core')->member_show($row['setmid']);
		$worker=pdo_get('jy_scrm_worker',array('id'=>$row['workerid'],'uniacid'=>$_SESSION['uniacid']));
		$row['worker']=array('mobile'=>$worker['alias'],'avatar'=>$worker['avatar'],'nickname'=>$worker['name']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'工单列表'),
		),
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'工单名称','type'=>'string'),
			array('name'=>'member','title'=>'客户','type'=>'member'),
			array('name'=>'worker','title'=>'所属员工','type'=>'member'),
			array('name'=>'credit','title'=>'工单积分','type'=>'string'),
			array('name'=>'groupid','title'=>'工单分类','type'=>'string'),
			array('name'=>'status','title'=>'工单状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'receive_type','title'=>'领取方式','type'=>'radio','options'=>'receive_types'),
			array('name'=>'receive_time','title'=>'领取时间','type'=>'time'),
			array('name'=>'complete_time','title'=>'完成时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'staff','title'=>'指派工单','show'=>array('status'=>0)),
			array('type'=>'href','op'=>'look','title'=>'查看工单','show'=>array('status !='=>4)),
			array('type'=>'href','op'=>'look','title'=>'不通过原因','show'=>array('status'=>4)),
		)),
		'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'groupid','title'=>'工单分类','option_title'=>'title','options'=>'groups'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'工单状态','options'=>'statuss'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'工单名称'),
		),
	);
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('jy_workliu_taskgroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['receive_types']=$this->receive_types;
	exi($return);
}

//提交工单
if($_RQ['op']=='complete'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'result'=>trim($post['result']),
			'status'=>2,
		);
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择工单！','error');
		}
		if(!$data['result']){
			exi('请先填写工单处理结果！','error');
		}
		pdo_update('jy_workliu_task',$data,array('id'=>$id));
		exi('提交成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择工单！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id),array('id','title','content'));
	if(!$item){
		exi('指定工单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'提交工单',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'工单名称','read'=>true),
			array('type'=>'string','name'=>'content','title'=>'工单内容','read'=>true),
			array('type'=>'text','name'=>'result','title'=>'处理结果','help'=>'这里填写工单处理结果。做些说明即可。如果是开发型工单，直接填写”处理完毕“即可。'),

		),
	);
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择工单！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id),array('groupid','qid','title','content','status','image'));
	$item['assess'] = $item['assess']? $item['assess'] : '还未审核！';
	$item['image']=$item['image']?json_decode($item['image'],true):array();
	$logs=pdo_getall('jy_workline_tcplog',array('tcpid'=>$item['tcpid']));
	if($logs){
		foreach($logs as &$v){
			if($v['setmid']==$item['getmid']){
				$v['isme']=0;
			}else{
				$v['isme']=1;
			}
			$v['uid']=$item['getmid'];
			$v['image']=json_decode($v['image'],true);
		}
	}
	$item['logs']=$logs;
	$workername=pdo_getcolumn('jy_scrm_worker',array('mid'=>$item['getmid'],'uniacid'=>$_SESSION['uniacid']),'name');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'工单详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('type'=>'string','read'=>true,'name'=>'title','title'=>'工单名称'),
			array('type'=>'text','read'=>true,'name'=>'content','title'=>'工单内容'),
			array('type'=>'images','read'=>true,'name'=>'image','title'=>'截图'),
			array('type'=>'logs','name'=>'logs','jsname'=>$workername,'title'=>'处理信息'),
			array('type'=>'radio','name'=>'status','title'=>'状态','options'=>'statuss'),
		),
	);
	exi($return);
}
//添加编辑工单
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'groupid'=>intval($post['groupid']),
			'content'=>trim($post['content']),
			'image'=>ijson_encode($post['image']),
			'credit'=>intval($post['credit']),
			'jiaji'=>intval($post['jiaji']),
		);
		$id=intval($post['id']);
		if($id){
			$tcpid = pdo_getcolumn('jy_workliu_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'tcpid');
			$tcplog = array('msg'=>trim($post['content']),'image'=>trim($post['image']));
			pdo_update('jy_workliu_task',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_workliu_tcplog',$tcplog,array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['setmid']=$_SESSION['uid'];
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_workliu_tcp',array('uniacid'=>$_SESSION['uniacid']));
			$tcpid=pdo_insertid();
			$data['tcpid']=$tcpid;
			pdo_insert('jy_workliu_task',$data);
			$tcplog = array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'msg'=>trim($post['content']),'image'=>trim($post['image']),'createtime'=>TIMESTAMP);
			pdo_insert('jy_workliu_tcplog',$tcplog);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_workliu_task',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['groups']=pdo_getall('jy_workliu_taskgroup',array(),array('id','title'),'id');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回工单列表'),
		),
		'desc'=>'设置工单信息',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'工单名称'),
			array('type'=>'switch','name'=>'jiaji','title'=>'是否加急'),
			array('type'=>'select','name'=>'groupid','title'=>'工单分组','options'=>'groups'),
			array('type'=>'radio','name'=>'type','title'=>'工单类型','options'=>'types'),
			array('type'=>'text','name'=>'content','title'=>'工单内容'),
			array('type'=>'images','name'=>'image','title'=>'工单截图'),
			array('type'=>'string','name'=>'credit','title'=>'工单积分'),
		),
	);
	exi($return);
}
//指派工单
if($_RQ['op']=='staff'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$item=pdo_get('jy_workliu_task',array('id'=>$id),array('id','title','tcpid'));
		if($item['setmid'] == $_SESSION['uid']){
			$receive_type = 1;
		}else{
			$receive_type = 3;
		}
		$getmid=pdo_getcolumn('jy_scrm_worker',array('id'=>$post['workerid'],'uniacid'=>$_SESSION['uniacid']),'mid');
		$data=array(
			'status'=>1,
			'receive_type'=>$receive_type,//1表示发布者指派
			'receive_time'=>TIMESTAMP,
			'workerid'=>$post['workerid'],
			'getmid'=>$getmid,
		);
		if(!$id){
			exi('请先选择工单！','error');
		}
		if(!$data['workerid']){
			exi('请先选择处理人！','error');
		}
		pdo_update('jy_workliu_task',$data,array('id'=>$id));
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择工单！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id),array('id','title','tcpid'));
	if(!$item){
		exi('指定工单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'指派特定人员处理工单',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'工单名称','read'=>true),
			array('type'=>'choose','title'=>'选择员工','name'=>'workerid','action'=>'jy_scrm/worker.get_worker/'),
		),
	);
	exi($return);
}